import { _decorator, Component, EventTouch, Node, SpriteFrame, Tween, tween, UITransform, v3, Vec3 } from 'cc';
import { Wear } from './Wear';
import { WearType } from './GameData';
import { MigaManager } from './MigaManager';
import { TweenTag } from './Subject';
const { ccclass, property } = _decorator;

@ccclass('Bandage')
export class Bandage extends Wear {

    @property([SpriteFrame])
    bandageSfs: SpriteFrame[] = [];

    TouchMove(event: EventTouch) {
        if (MigaManager.Instance.tutorial.active) return;
        super.TouchMove(event);
        if (this.personTarget) {
            let shoe = this.personTarget.node.children[26];
            if (shoe.getComponent(UITransform).getBoundingBoxToWorld().contains(event.getUILocation())) {
                this.canWear = true;
                this.ChangeBandage(3);
            }
            else {
                this.canWear = false;
                this.ChangeBandage(0);
            }
        }
        else if (this.handPerson) {
            this.canWear = true;
            this.ChangeBandage(2);
        }
        else if (this.headPerson) {
            let node = this.headPerson.node.children[12];
            if (node.getComponent(UITransform).getBoundingBoxToWorld().contains(event.getUILocation())) {
                this.canWear = true;
                this.ChangeBandage(1);
            }
            else {
                this.canWear = false;
                this.ChangeBandage(0);
            }
        }
        else {
            this.canWear = false;
            this.ChangeBandage(0);
        }
    }

    TouchEnd(event: EventTouch): void {
        if (this.isTween) return;
        if (!this.isTouch) return;
        if (MigaManager.Instance.tutorial.active) return;
        Tween.stopAllByTag(TweenTag.Touch, this.node);
        tween(this.node).tag(TweenTag.Touch)
            .to(0.1, { scale: Vec3.ONE })
            .start();
        this.touchUpPos = this.node.getPosition();
        this.isTouch = false;
        if (this.canWear) {
            let num = this.bandageSfs.indexOf(this.sprite.spriteFrame);
            switch (num) {
                case 0:
                    console.error("绷带有问题");
                    break;
                case 1:
                    console.error("绷带有问题");
                    break;
                case 2:
                    console.error("绷带有问题");
                    break;
                case 3:
                    console.error("绷带有问题");
                    break;
            }
        }
        else if (this.sitTarget) {
            this.node.setParent(this.sitTarget, true);
            let otherWidth = this.sitTarget.getComponent(UITransform).width;
            let delta = this.sitTarget.getWorldPosition().x - this.node.getWorldPosition().x;
            let pos = v3(this.node.getWorldPosition().x, this.sitTarget.getWorldPosition().y);
            if (otherWidth < this.uiTransform.width) pos.x = this.sitTarget.getWorldPosition().x;
            else if (delta > otherWidth / 2 - this.width / 2) pos.x = this.sitTarget.getWorldPosition().x - otherWidth / 2 + this.width / 2;
            else if (delta < this.width / 2 - otherWidth / 2) pos.x = this.sitTarget.getWorldPosition().x + otherWidth / 2 - this.width / 2;
            Tween.stopAllByTag(TweenTag.FallCollid, this.node);
            tween(this.node).tag(TweenTag.FallCollid)
                .to(0.2, { worldPosition: pos })
                .start();
        }
        else {
            this.Raycast(event.getUILocation());
        }
    }

    ChangeBandage(num: number) {
        this.sprite.spriteFrame = this.bandageSfs[num];
        this.collider.size.width = this.uiTransform.width;
        this.collider.size.height = this.uiTransform.height;
        this.collider.apply();
    }

}


